package org.xinjiang.tarim.travel.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xinjiang.tarim.travel.model.LookMddRecord;
import org.xinjiang.tarim.travel.model.PoiMddWithBLOBs;
import org.xinjiang.tarim.travel.service.AttentionMddService;
import org.xinjiang.tarim.travel.service.LookMddRecordService;
import org.xinjiang.tarim.travel.service.PoiMddService;
import org.xinjiang.tarim.travel.service.PoiService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/mdd")
public class PoiMddApi {
    @Autowired
    PoiMddService poiMddService;
    @Autowired
    PoiService poiService;
    @Autowired
    LookMddRecordService lookMddRecordService;
    @Autowired
    AttentionMddService attentionMddService;

    /**
     * 获取目的地列表
     * @param pageIndex
     * @param pageSize
     * @param searchName
     * @return
     */
    @RequestMapping(value = "/getMdd")
    public JsonResult getMdd(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "searchName", defaultValue = "") String searchName){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("searchName", searchName);
            pageBean = poiMddService.selectByCondition(criteria, pageBean);
            if(pageBean.getResultList().size() == 0){
                jsonResult = new JsonResult(Constants.STATUS_NOT_FOUND, "查询列表失败");
            }else{
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 查全部
     * @param searchName
     * @return
     */
    @RequestMapping(value = "/getAll")
    public JsonResult getAll(@RequestParam(value = "searchName", defaultValue = "") String searchName){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("searchName", searchName);
            List list = poiMddService.selectAll(criteria);
            if(list.size() == 0){
                jsonResult = new JsonResult(Constants.STATUS_NOT_FOUND, "查询列表失败");
            }else{
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", list);
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 获取详情
     * @param mddId
     * @return
     */
    @RequestMapping(value = "/getDetail")
    public JsonResult getDetail(HttpServletRequest request, @RequestParam(value = "mddId") String mddId){
        JsonResult jsonResult;
        try {
            String openId = request.getHeader("tokenId");
            jsonResult = poiMddService.selectByMddId(mddId, openId);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }
}
